package gfxengine;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
import java.util.Vector;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class TabuleiroSwing implements Observer {

	private JFrame frame;
	private model.World w = null;
	private GameScreen gui;
	private int cellsize = 15;
	private int lado;
	private int cobra_id = -1;
	private int destino = -1;

	/*
	 * @Override public void run() { this.update(w, w.getClock() );
	 * 
	 * // TODO Auto-generated method stub
	 * 
	 * }
	 */

	public class GameScreen extends JComponent {

		public GameScreen() {
			super();
		}

		// @Override
		public void paint(Graphics g) {

			for (int i = 0; i != lado * lado; ++i) {
				// desenha quadrados de cellsize pixeis por cellsize pixeis

				// representa bonus e obstaculos

				switch (w.getCelula((Integer) i).getBonus()) {

				case BLOCK:
					g.setColor(Color.WHITE);
					g.fillRect(i % lado * cellsize, (i / lado * cellsize)
							+ cellsize, cellsize, cellsize);
					g.setColor(Color.GREEN);
					g.drawRect(i % lado * cellsize, (i / lado * cellsize)
							+ cellsize, cellsize, cellsize);
					g.setColor(Color.GREEN);
					g.drawRect(i % lado * cellsize, (i / lado * cellsize)
							+ cellsize, cellsize, cellsize);
					break;
				case FOOD:
					g.setColor(Color.BLACK);
					g.fillRect(i % lado * cellsize, (i / lado * cellsize)
							+ cellsize, cellsize, cellsize);
					g.setColor(Color.RED);
					g.drawString("F", i % lado * cellsize + 2, (cellsize + i
							/ lado * cellsize) + 13);
					g.setColor(Color.GREEN);
					g.drawRect(i % lado * cellsize, (i / lado * cellsize)
							+ cellsize, cellsize, cellsize);
					break;
				case TURBO:
					g.setColor(Color.BLACK);
					g.fillRect(i % lado * cellsize, (i / lado * cellsize)
							+ cellsize, cellsize, cellsize);
					g.setColor(Color.MAGENTA);
					g.drawString("T", i % lado * cellsize + 2, (cellsize + i
							/ lado * cellsize) + 13);
					g.setColor(Color.GREEN);
					g.drawRect(i % lado * cellsize, (i / lado * cellsize)
							+ cellsize, cellsize, cellsize);
					break;
				case JUMP:
					g.setColor(Color.BLACK);
					g.fillRect(i % lado * cellsize, (i / lado * cellsize)
							+ cellsize, cellsize, cellsize);
					g.setColor(Color.CYAN);
					g.drawString("!!!", i % lado * cellsize + 2, (cellsize + i
							/ lado * cellsize) + 13);
					g.setColor(Color.GREEN);
					g.drawRect(i % lado * cellsize, (i / lado * cellsize)
							+ cellsize, cellsize, cellsize);
					break;
				case NONE:
					g.setColor(Color.BLACK);
					g.fillRect(i % lado * cellsize, (i / lado * cellsize)
							+ cellsize, cellsize, cellsize);
					g.setColor(Color.GREEN);
					g.drawRect(i % lado * cellsize, (i / lado * cellsize)
							+ cellsize, cellsize, cellsize);
					g.setColor(Color.GREEN);
					g.drawRect(i % lado * cellsize, (i / lado * cellsize)
							+ cellsize, cellsize, cellsize);
					break;

				}
				;

			}

			// pinta cobras, modificar par ao novo vector

			for (int iii = 0; iii <= w.max_cobras; ++iii) {
				if (w.celulas_cobras.containsKey(iii)) {
					// System.out.println(iii);

					for (int ii = 0; ii != w.getCelulasCobra(iii).length(); ++ii) {
						int i = w.getCelulasCobra(iii).get(ii);

						if (w.get_salto_da_cobra(iii) == true) {
							g.setColor(new Color(10, 10, 255));

						} else {
							g.setColor(new Color(255, 205 / w.max_cobras * iii
									+ 50, 205 / w.max_cobras * iii + 50));
							// cor diferente para quem tem salto

						}
						g.fillRect(i % lado * cellsize, (i / lado * cellsize)
								+ cellsize, (cellsize - 4), (cellsize - 4));

					}
				}
			}
		}
	}

	public int traduzclick(int x, int y) {
		int celula;
		celula = (y / cellsize * lado + x / cellsize);
		// System.out.println(celula);
		return celula;
	}

	public TabuleiroSwing(final model.World w) {

		frame = new JFrame("Tabuleiro");
		this.w = w;
		this.lado = w.getLado();
		// frame.setLayout(new GridLayout());
		frame.setBounds(100, 50, lado * cellsize, lado * cellsize + 60);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.gui = new GameScreen();
		frame.getContentPane().add(gui);
		frame.setVisible(true);
		frame.setResizable(false);
		gui.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub

				// !!!martelado para o topo dar destinos certos;
				// !!!while(origem!=w.celulas_cobras. (cobra_id))
				//origem = traduzclick(e.getX(), Math.abs(e.getY() - cellsize));
				if (w.isCobrainCell(traduzclick(e.getX(), Math.abs(e.getY() - cellsize)))) {
					cobra_id = w.getCobrainCell(traduzclick(e.getX(), Math.abs(e.getY() - cellsize)));
				}

				if(cobra_id != -1 && !w.isCobrainCell(traduzclick(e.getX(), Math.abs(e.getY() - cellsize)))){
					destino=traduzclick(e.getX(), Math.abs(e.getY() - cellsize));
				}
				//System.out.println("Cobra " + cobra_id + " com destino " +  destino);
				

				
				if(cobra_id != -1 && destino != -1){
					w.destinos_cobras.put(cobra_id, w.criaDestinos(w.celulas_cobras.get(cobra_id).get(0), destino));
					//System.out.println(w.destinos_cobras);
					
				}
				
				

			}
		});

	}

	@Override
	public void update(Observable arg0, Object arg1) {
		// System.out.println("Update called!");
		gui.revalidate();
		gui.repaint();
	}

}
